package client_V2;
// <editor-fold defaultstate="collapsed" desc="Imports">
import lib.FileHandler;
import lib.StreamSocket;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
// </editor-fold>

/**
 * @file ClientDataTransferThread.java
 *
 * @version 0.1
 * @author Chris Sewell 10008342
 */
public class ClientDataTransferThread extends Thread {

    // <editor-fold defaultstate="collapsed" desc="Attributes">
    StreamSocket serverFileStream;
    FileHandler file;
    byte[] data;
    int length;
// </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Constructors">

    public ClientDataTransferThread(StreamSocket serverFileStream, int length, FileHandler file) throws UnknownHostException, IOException {
        //Prepare stream and data for transfer
        this.serverFileStream = serverFileStream;
        this.length = length;
        this.file = file;
        data = new byte[this.length];
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Methods">

    @Override
    public void run() {
        data = serverFileStream.recieveBytes(length);//Receive the file!
        try {
            serverFileStream.kill();
        } catch (IOException ex) {
            Logger.getLogger(ClientDataTransferThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        file.writeToFIle(data);//Write to new file
    }
    // </editor-fold> 
}